{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import linora as la"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "corpus = [\n",
    "    'This is the first document.',\n",
    "    'This document is the second document.',\n",
    "    'And this is the third one.',\n",
    "    'Is this the first document?',\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# filter punctuation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['This is the first document',\n",
       " 'This document is the second document',\n",
       " 'And this is the third one',\n",
       " 'Is this the first document']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "corpus = la.text.filter_punctuation(corpus)\n",
    "corpus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['This', 'is', 'the', 'first', 'document'],\n",
       " ['This', 'document', 'is', 'the', 'second', 'document'],\n",
       " ['And', 'this', 'is', 'the', 'third', 'one'],\n",
       " ['Is', 'this', 'the', 'first', 'document']]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "corpus = [i.split(' ') for i in corpus]\n",
    "corpus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CountVectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, scale = la.text.CountVectorizer(corpus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns_this</th>\n",
       "      <th>columns_second</th>\n",
       "      <th>columns_Is</th>\n",
       "      <th>columns_one</th>\n",
       "      <th>columns_document</th>\n",
       "      <th>columns_third</th>\n",
       "      <th>columns_first</th>\n",
       "      <th>columns_the</th>\n",
       "      <th>columns_is</th>\n",
       "      <th>columns_This</th>\n",
       "      <th>columns_And</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   columns_this  columns_second  columns_Is  columns_one  columns_document  \\\n",
       "0             0               0           0            0                 1   \n",
       "1             0               1           0            0                 2   \n",
       "2             1               0           0            1                 0   \n",
       "3             1               0           1            0                 1   \n",
       "\n",
       "   columns_third  columns_first  columns_the  columns_is  columns_This  \\\n",
       "0              0              1            1           1             1   \n",
       "1              0              0            1           1             1   \n",
       "2              1              0            1           1             0   \n",
       "3              0              1            1           0             0   \n",
       "\n",
       "   columns_And  \n",
       "0            0  \n",
       "1            0  \n",
       "2            1  \n",
       "3            0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['this',\n",
       " 'second',\n",
       " 'Is',\n",
       " 'one',\n",
       " 'document',\n",
       " 'third',\n",
       " 'first',\n",
       " 'the',\n",
       " 'is',\n",
       " 'This',\n",
       " 'And']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scale"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TfidfVectorizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns_this</th>\n",
       "      <th>columns_second</th>\n",
       "      <th>columns_Is</th>\n",
       "      <th>columns_one</th>\n",
       "      <th>columns_document</th>\n",
       "      <th>columns_third</th>\n",
       "      <th>columns_first</th>\n",
       "      <th>columns_the</th>\n",
       "      <th>columns_is</th>\n",
       "      <th>columns_This</th>\n",
       "      <th>columns_And</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.418127</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.516470</td>\n",
       "      <td>0.341846</td>\n",
       "      <td>0.418127</td>\n",
       "      <td>0.516470</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.504371</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.643868</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.263202</td>\n",
       "      <td>0.321934</td>\n",
       "      <td>0.397652</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.380147</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.482169</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.482169</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.251616</td>\n",
       "      <td>0.307762</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.482169</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.461153</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.584914</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.373343</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.461153</td>\n",
       "      <td>0.305232</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   columns_this  columns_second  columns_Is  columns_one  columns_document  \\\n",
       "0      0.000000        0.000000    0.000000     0.000000          0.418127   \n",
       "1      0.000000        0.504371    0.000000     0.000000          0.643868   \n",
       "2      0.380147        0.000000    0.000000     0.482169          0.000000   \n",
       "3      0.461153        0.000000    0.584914     0.000000          0.373343   \n",
       "\n",
       "   columns_third  columns_first  columns_the  columns_is  columns_This  \\\n",
       "0       0.000000       0.516470     0.341846    0.418127      0.516470   \n",
       "1       0.000000       0.000000     0.263202    0.321934      0.397652   \n",
       "2       0.482169       0.000000     0.251616    0.307762      0.000000   \n",
       "3       0.000000       0.461153     0.305232    0.000000      0.000000   \n",
       "\n",
       "   columns_And  \n",
       "0     0.000000  \n",
       "1     0.000000  \n",
       "2     0.482169  \n",
       "3     0.000000  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "la.text.TfidfVectorizer(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# word count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'This': 2,\n",
       "         'is': 3,\n",
       "         'the': 4,\n",
       "         'first': 2,\n",
       "         'document': 4,\n",
       "         'second': 1,\n",
       "         'And': 1,\n",
       "         'this': 2,\n",
       "         'third': 1,\n",
       "         'one': 1,\n",
       "         'Is': 1})"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_count_dict = la.text.word_count(corpus)\n",
    "word_count_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# low freq word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['second', 'And', 'third', 'one', 'Is']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "la.text.word_low_freq(word_count_dict, threshold=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# high freq word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['is', 'the', 'document']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "la.text.word_high_freq(word_count_dict, threshold=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# filter word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['This', 'is', 'the', 'first', 'document'],\n",
       " ['This', 'document', 'is', 'the', 'document'],\n",
       " ['this', 'is', 'the'],\n",
       " ['this', 'the', 'first', 'document']]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "la.text.filter_word(corpus, la.text.word_low_freq(word_count_dict, threshold=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# word to index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'this': 1,\n",
       " 'second': 2,\n",
       " 'Is': 3,\n",
       " 'one': 4,\n",
       " 'document': 5,\n",
       " 'third': 6,\n",
       " 'first': 7,\n",
       " 'the': 8,\n",
       " 'is': 9,\n",
       " 'This': 10,\n",
       " 'And': 11}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_index_dict = la.text.word_to_index(corpus)\n",
    "word_index_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# word index sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[10, 9, 8, 7, 5], [10, 5, 9, 8, 2, 5], [11, 1, 9, 8, 6, 4], [3, 1, 8, 7, 5]]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_index_sequence = la.text.word_index_sequence(corpus, word_index_dict)\n",
    "word_index_sequence"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# select best length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "la.text.select_best_length(corpus, sample_rate=0.7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pad sequences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0, 10, 9, 8, 7, 5],\n",
       " [10, 5, 9, 8, 2, 5],\n",
       " [11, 1, 9, 8, 6, 4],\n",
       " [0, 3, 1, 8, 7, 5]]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_index_sequence = la.text.pad_sequences(word_index_sequence, la.text.select_best_length(corpus, sample_rate=0.7))\n",
    "word_index_sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
